"""
教师接口层
"""
from db.models import Teacher, Student, Course
from lib.utils import prettyprint


def select_course_interface(teacher_name: str, course_name: str) -> (bool, str):
    """教师选择课程接口"""
    teacher_obj = Teacher.read(teacher_name)
    # 课程存在
    if course_name in teacher_obj.course_name_list:
        return False, '此课程已经被选择！'
    # 选择课程
    teacher_obj.course_name_list.append(course_name)
    teacher_obj.save()
    return True, f'[{teacher_name}]成功选择[{course_name}]课程！'


def check_course_interface(teacher_name):
    """教师查看课程接口"""
    teacher_obj = Teacher.read(teacher_name)
    if not teacher_obj.course_name_list:
        return False, '当前教师下没有课程，请先选择课程'
    prettyprint(title='已选择课程', fields=['课程名'],  # 打印
                data=[[course_name] for course_name in teacher_obj.course_name_list],
                auto_index=True)

    return True, '已选课程见上表'


def list_course_from_teacher(teacher_name) -> list[str]:
    """查看教师已选择的课程"""
    teacher_obj = Teacher.read(teacher_name)
    return teacher_obj.course_name_list


def change_score_interface(course_name, student_name, score):
    """教师修改分数接口"""
    course_obj = Course.read(course_name)  # 获取课程
    student_obj = Student.read(student_name)  # 获取学生
    student_obj.score_dict[course_name] = score  # 修改学生成绩
    for student in course_obj.student_list:
        if student.name == student_name:
            student.score_dict[course_name] = score  # 修改课程中学生的成绩
    # 保存数据
    student_obj.save()
    course_obj.save()
    return True, f'成功修改{student_name}的{course_name}为{score}!'
